import '../helper/Helper.dart';
import '../helper/uidata.dart';

import '../helper/dioHelper2.dart';

class SpaceX {
  int ID;
  String Guid;
  String TopId;
  String Title;
  String Description;
  String Field1;
  String Field2;
  String Field3;
  String CreateTM;
  int State;
  String AuthRead;
  String AuthWrite;
  String CreateUser;

  SpaceX(
      this.ID,
      this.Guid,
      this.TopId,
      this.Title,
      this.Description,
      this.Field1,
      this.Field2,
      this.Field3,
      this.CreateTM,
      this.State,
      this.AuthRead,
      this.AuthWrite,
      this.CreateUser);
  SpaceX.fromJson(Map<String, dynamic> json)
      : ID = json["ID"],
        Guid = json["Guid"],
        TopId = json["TopId"],
        Title = json["Title"],
        Description = json["Description"],
        Field1 = json["Field1"],
        Field2 = json["Field2"],
        Field3 = json["Field3"],
        CreateTM = json["CreateTM"],
        State = json["State"],
        AuthRead = json["AuthRead"],
        AuthWrite = json["AuthWrite"],
        CreateUser = json["CreateUser"];
  Map<String, dynamic> toJson() => {
        'ID': ID,
        'Guid': Guid,
        'TopId': TopId,
        'Title': Title,
        'Description': Description,
        'Field1': Field1,
        'Field2': Field2,
        'Field3': Field3,
        'CreateTM': CreateTM,
        'State': State,
        'AuthRead': AuthRead,
        'AuthWrite': AuthWrite,
        'CreateUser': CreateUser,
      };

  static SpaceX NewX(String guid, String top, String title) {
    return new SpaceX(0, guid, top, title, "", "", "", "",
        Helper.DateTimeNowString(), 0, "", "", UIData.CurrentUser!.Guid);
  }

  static Future<bool> Add(SpaceX x1) async {
    var response = await DioHelper.Post("/SpaceX/Add", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Update(SpaceX x1) async {
    var response = await DioHelper.Post("/SpaceX/Update", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<bool> Delete(SpaceX x1) async {
    var st = x1.toJson();
    var response = await DioHelper.Post("/SpaceX/Delete", x1.toJson());
    if (response.statusCode == 200) return true;
    return false;
  }

  static Future<SpaceX?> Get(String where, dynamic data) async {
    var response = await DioHelper.Post("/SpaceX/GetByWhere", {
      "Where": where,
      "obj": data,
    });

    if (response.statusCode == 200) return new SpaceX.fromJson(response.data);
    return null;
  }

  static Future<List<SpaceX>> GetList(
      String id, int pageIndex, int pageSize) async {
    String p1 = pageIndex.toString();
    String ps = pageSize.toString();

    var response =
        await DioHelper.Get("/SpaceX/GetList?SpaceId=$id&p1=$p1&ps=$ps", {});

    var list1 = <SpaceX>[];
    if (response.statusCode == 200) {
      if (response.data == null) return list1;
      for (var x in response.data) {
        list1.add(new SpaceX.fromJson(x));
      }
    }
    return list1;
  }

  static Future<List<SpaceX>> GetAllList() async {
    var response = await DioHelper.Get("/SpaceX/GetAllList", {});
    var list1 = <SpaceX>[];
    if (response.statusCode == 200) {
      for (var x in response.data) {
        list1.add(new SpaceX.fromJson(x));
      }
    }
    return list1;
  }
}
